const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const webpack = require("webpack"); //增加导入webpack

function resolve(dir) {
  return path.resolve(__dirname, "..", dir);
}

module.exports = {
  mode: "development",
  devtool: "cheap-module-source-map",
  devServer: {
    hot: true, //在devServer中增加hot字段
    contentBase: resolve("src"),
    publicPath: "/",
    host: "127.0.0.1",
    port: 3000,
    stats: {
      colors: true,
    },
  },
  entry: [resolve("src/app.tsx"), resolve("src/dev.js")], //在entry字段中添加触发文件配置
  resolve: {
    extensions: [".wasm", ".mjs", ".js", ".tsx", ".ts", ".json", ".jsx"],
    // 设置别名
    alias: {
      "@": resolve("src"),
      "@packages": resolve("packages"),
    },
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/, // 排除 node_modules 文件夹
        use: {
          // loader 是 babel
          loader: "babel-loader",
          options: {
            // babel 转义的配置选项
            cacheDirectory: true,
          },
        },
      },
    ],
  },
  plugins: [
    // plugins中增加下面内容，实例化热加载插件
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebPackPlugin({
      template: "public/index.html",
      filename: "index.html",
      inject: true,
    }),
  ],
};
